Component({
  properties:{
    dateInfo: Object
  },
  data:{
    hide: true,
    scheduleTypes:[
      "学习",
      "讲授",
      "检查",
      "简化"
    ],
    index: 0,
    miniSchedule:{
      type: "学习",
      week: "",
      year: "",
      month: "",
      day: "",
      content: "",
      startTime: "06:00",
      endTime: "12:00"
    }
  },
  methods:{
    scheduleTypeChange: function(event){
      this.setData({
        index: event.detail.value,
        ["miniSchedule.type"]: this.data.scheduleTypes[event.detail.value]
      })
    },
    confirmContent: function(event){
      this.setData({
        ["miniSchedule.content"]: event.detail.value
      })
    },
    startTimeChange: function(event){
      this.setData({
        ["miniSchedule.startTime"]: event.detail.value
      })
    },
    endTimeChange: function(event){
      this.setData({
        ["miniSchedule.endTime"]: event.detail.value
      })
    },
    showPopup: function(){
      this.setData({
        hide: false
      })
    },
    hidePopup: function(){
      this.setData({
        hide: true
      })
    },
    confirmAll: function(){
      var st = new Date("2000-01-01 "+this.data.miniSchedule.startTime+":00")
      var et = new Date("2000-01-01 "+this.data.miniSchedule.endTime+":00")
      if(st >= et){
        wx.showToast({
          title: '请输入正确时间哟~',
          icon: 'none',
          duration: 1500
        })
        return
      }
      const dp = wx.cloud.database()
      var newSchedule = {
        type: this.data.miniSchedule.type,
        week: this.data.dateInfo.week,
        year: this.data.dateInfo.year,
        month: this.data.dateInfo.month,
        day: this.data.dateInfo.day,
        content: this.data.miniSchedule.content,
        startTime: this.data.miniSchedule.startTime,
        endTime: this.data.miniSchedule.endTime
      }
      var app = getApp()
      var utils = require("../../utils/utils.js")
      app.userSchedules[utils.weekToIndex(this.data.dateInfo.week)].push(newSchedule)
      dp.collection("schedule").add({
        data: newSchedule,
        success: function(){
          wx.showToast({
            title: '新增计划成功~',
          })
        },
        fail: err => {
          wx.showToast({
            title: '错误！'
          })
        }
      })
      this.hidePopup()
    }
  }
  
})